package com.VolunteerPlatform.Controller.volunteer;

import com.VolunteerPlatform.Entity.VolunteerRecord;
import com.VolunteerPlatform.Entity.VolunteerSign;
import com.VolunteerPlatform.Service.VolunteerService.VolunteerRecordService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by DoveXL on 2018/12/18.
 */
@Controller
@RequestMapping(value = "/VolunteerRecord/**")
public class VolunteerRecordController {

    @Autowired
    private VolunteerRecordService volunteerRecordService;


    @RequestMapping(value = "checkVolunteerRecordInfo", method = RequestMethod.POST)
    @ResponseBody
    public String checkVolunteerRecord(@RequestBody String jsondata, HttpSession httpSession)
    {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCard = jsonObject.getString("IDCardNumber");
        String TeamName = jsonObject.getString("TeamName");
        String StartDate= jsonObject.getString("Date");
        String EndDate= jsonObject.getString("Date");
        List<VolunteerRecord> volunteerRecordList = volunteerRecordService.getAllVolunteerRecord(IDCard,null,TeamName,StartDate,EndDate,null,null,-1,-1);
        if(volunteerRecordList.size()==0)
        {
            return "1";
        }else
        {
            return "0";
        }
    }

    @RequestMapping(value = "AddVolunteerRecordInfo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String AddVolunteerRecord(@RequestBody String jsondata, HttpSession httpSession)
    {
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
        String User = securityContextImpl.getAuthentication().getName();
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCardNumber = jsonObject.getString("IDCardNumber");
        String Name = jsonObject.getString("Name");
        String DateDay = jsonObject.getString("Date");
        String StartTime = jsonObject.getString("StartTime");
        String EndTime = jsonObject.getString("EndTime");
        int TeamNumber = jsonObject.getIntValue("TeamNumber");
        String TeamName = jsonObject.getString("TeamName");
        String DateStart = DateDay+" "+StartTime+":00";
        String DateEnd = DateDay+" "+EndTime+":00";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        double duration=0.0;
        try
        {
            Date d1 = df.parse(DateStart);
            Date d2 = df.parse(DateEnd);
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
            duration = diff / (1000 * 60 * 60);
        }catch (Exception e)
        {
            duration=0.0;
            e.printStackTrace();
        }
        int flag = volunteerRecordService.insertVolunteerRecord(IDCardNumber,Name,TeamName,DateDay,StartTime,EndTime,TeamNumber,duration,User);
        if(flag==1)
        {
            JSONObject object = new JSONObject();
            object.put("IDCardNumber",IDCardNumber);
            object.put("Name",Name);
            object.put("TeamName",TeamName);
            object.put("Status","已录入");
            return JSON.toJSONString(object);
        }else
        {
            JSONObject object = new JSONObject();
            object.put("IDCardNumber",IDCardNumber);
            object.put("Name",Name);
            object.put("TeamName",TeamName);
            object.put("Status","未录入");
            return JSON.toJSONString(object);
        }
    }

    @RequestMapping(value = "VolunteerRecordInfo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String VolunteerRecordInfo(@RequestBody String jsondata, HttpSession httpSession)
    {
        int rowCount=0;
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String Name = jsonObject.getString("Name");
        String StartDay = jsonObject.getString("StartDay");
        String EndDay = jsonObject.getString("EndDay");
        int page = jsonObject.getInteger("page");
        int pagesize = jsonObject.getIntValue("pagesize");
        String Unit = jsonObject.getString("Unit");
        JSONArray UserInfoArray = new JSONArray();
        List<VolunteerRecord> volunteerRecordList = volunteerRecordService.getAllVolunteerRecord(null,Name,null,StartDay,EndDay,Unit,null,-1,-1);
        try {
            rowCount = volunteerRecordList.size();
            System.out.println(rowCount);
            if (rowCount%pagesize != 0) {
                rowCount = rowCount/pagesize + 1;
            } else {
                rowCount = rowCount /pagesize;
            }
            System.out.println(rowCount);
            List<VolunteerRecord> volunteerRecords = volunteerRecordService.getAllVolunteerRecord(null,Name,null,StartDay,EndDay,Unit,null,(page-1)*pagesize,pagesize);

            for (int i=0;i<volunteerRecords.size();i++) {
                JSONObject UserInfo = new JSONObject();
                UserInfo.put("IDCardNumber", volunteerRecords.get(i).getIDCardNumber());
                UserInfo.put("Name", volunteerRecords.get(i).getName());
                UserInfo.put("DateDay", volunteerRecords.get(i).getDate());
                UserInfo.put("TeamName", volunteerRecords.get(i).getTeamName());
                UserInfo.put("TeamNumber", volunteerRecords.get(i).getTeamNumber());
                UserInfo.put("Time", volunteerRecords.get(i).getStartTime()+"-"+volunteerRecords.get(i).getEndTime());
                UserInfoArray.add(UserInfo);
            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("pagecount",rowCount);
        jsonObject1.put("currentpage",page);
        jsonObject1.put("pagelist",UserInfoArray);
        return JSON.toJSONString(jsonObject1);
    }



    @RequestMapping(value = "ChangeVolunteerRecordInfo", method = RequestMethod.POST)
    @ResponseBody
    public String ChangeVolunteerRecordInfo(@RequestBody String jsondata,HttpSession httpSession)
    {
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
        String User = securityContextImpl.getAuthentication().getName();
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCard = jsonObject.getString("IDCardNumber");
        String Name = jsonObject.getString("Name");
        String OriginDate = jsonObject.getString("OriginDate");
        String NewDate = jsonObject.getString("NewDate");
        String OriginTeamName = jsonObject.getString("OriginTeamName");
        String NewTeamName = jsonObject.getString("NewTeamName");
        int NewTeamNumber = jsonObject.getIntValue("NewTeamNumber");
        String NewStartTime = jsonObject.getString("NewStartTime");
        String NewEndTime = jsonObject.getString("NewEndTime");
        String DateStart = NewDate+" "+NewStartTime+":00";
        String DateEnd = NewDate+" "+NewEndTime+":00";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        double duration=0.0;
        try
        {
            Date d1 = df.parse(DateStart);
            Date d2 = df.parse(DateEnd);
            long diff = d2.getTime() - d1.getTime();//这样得到的差值是微秒级别
            duration = 1.0*diff/(1000.0 * 60 * 60);
            System.out.println(diff+" "+duration);
        }catch (Exception e)
        {
            duration=0.0;
            e.printStackTrace();
        }
        List<VolunteerRecord> list = volunteerRecordService.getAllVolunteerRecord(IDCard,Name,OriginTeamName,OriginDate,OriginDate,null,null,-1,-1);
        System.out.println(list.size());
        int flag =volunteerRecordService.updateVolunteerRecord(IDCard,Name,OriginTeamName,NewTeamName,OriginDate,NewDate,NewStartTime,NewEndTime,NewTeamNumber,duration,User);
        if(flag==list.size())
        {
            return "true";
        }else
        {
            return "false";
        }
    }



    @RequestMapping(value = "DeleteVolunteerRecordInfo", method = RequestMethod.POST)
    @ResponseBody
    public String ChangeVolunteerRecordInfo(@RequestBody String jsondata)
    {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCard = jsonObject.getString("IDCardNumber");
        String Name = jsonObject.getString("Name");
        String Date = jsonObject.getString("Date");
        String TeamName = jsonObject.getString("TeamName");
        int flag = volunteerRecordService.deleteVolunteerRecord(IDCard,TeamName,Date);
        if(flag==1)
        {
            return "true";
        }
        else
        {
            return "false";
        }
    }













}
